package org.whh.auth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.whh.auth.WhhPermission;
import org.whh.auth.service.ILoginService;
import org.whh.base.util.AjaxResult;
import org.whh.base.util.LoginMap;
import org.whh.dto.LoginDTO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/login")
@WhhPermission(name="登录权限",desc = "这是登录权限")
public class LoginController {
    @Autowired
    ILoginService loginService;

    @PostMapping
    public AjaxResult login(@RequestBody LoginDTO loginDTO){
        try {
            Map<String,Object> loginMap=loginService.login(loginDTO);
            return AjaxResult.getself().setMessage("登录成功").setObj(loginMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("登录失败"+e.getMessage());
        }
    }

    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        String token = request.getHeader("token");
        LoginMap.loginMap.remove(token);
        return AjaxResult.getself().setMessage("成功退出");

    }
}
